﻿<?php
/* * ***************************************************************************
 * To access this WSDL specification run via: /wsdl.php?WSDL
 * Any other access to this WSDL will display as a HTML document
 * 
 * 2013 (C) Copyright Lyndon Leverington / DarkerWhite
 * ****************************************************************************
 * Set up the web service parameters:
 * $serviceName: Plain Text to display when someone accesses this service 
 *               without the ?WSDL parameter in the URL. Whitespaces are
 *               removed from this and this is then used as the ID for the 
 *               XML in the WSDL. Please only use A-Z, 0-9 and spaces.
 *
 * Declare all Functions for this Web Service
 * $functions Array Parameters:
 *  funcName - Name of the particular function being served
 *  doc - Documentation to report from Web Service regarding this function
 *  inputParams - An array of arrays where name = name of field and type = data type
 *                  Omit if not required
 *  outputParams - As above, but for responses
 *                  Omit if not required
 *  soapAddress - The php file to send to to process SOAP requests                
 * *************************************************************************** */

// ----------------------------------------------------------------------------
// END OF PARAMETERS SET UP
// ----------------------------------------------------------------------------

/* * ***************************************************************************
 * Process Page / Request
 * *************************************************************************** */

if (stristr($_SERVER['QUERY_STRING'], "wsdl")) {
    // WSDL request - output raw XML
    header("Content-Type: application/soap+xml; charset=utf-8");
    echo $this->DisplayXML();
} else {
    // Page accessed normally - output documentation
    $cp = substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1); // Current page
    echo '<!-- Attention: To access via a SOAP client use ' . $cp . '?WSDL -->';
    echo '<html>';
    echo '<head><title>' . $serviceName . '</title></head>';
    echo '<body>';
    echo '<h1>' . $serviceName . '</h1>';
    echo '<p style="margin-left:20px;">To access via a SOAP client use <code>' . $cp . '?WSDL</code></p>';

    // Document each function
    echo '<h2>Available Functions:</h2>';
    echo '<div style="margin-left:20px;">';
    for ($i = 0; $i < count($functions); $i++) {
        echo '<h3>Function: ' . $functions[$i]['funcName'] . '</h3>';
        echo '<div style="margin-left:20px;">';
        echo '<p>';
        echo $functions[$i]['doc'];
        echo '<ul>';
        if (array_key_exists("inputParams", $functions[$i])) {
            echo '<li>Input Parameters:<ul>';
            for ($j = 0; $j < count($functions[$i]['inputParams']); $j++) {
                echo '<li>' . $functions[$i]['inputParams'][$j]['name'];
                echo ' (' . $functions[$i]['inputParams'][$j]['type'];
                echo ')</li>';
            }
            echo '</ul></li>';
        }
        if (array_key_exists("outputParams", $functions[$i])) {
            echo '<li>Output Parameters:<ul>';
            for ($j = 0; $j < count($functions[$i]['outputParams']); $j++) {
                echo '<li>' . $functions[$i]['outputParams'][$j]['name'];
                echo ' (' . $functions[$i]['outputParams'][$j]['type'];
                echo ')</li>';
            }
            echo '</ul></li>';
        }
        echo '</ul>';
        echo '</p>';
        echo '</div>';
    }
    echo '</div>';

    echo '<h2>WSDL output:</h2>';
    echo '<pre style="margin-left:20px;width:800px;overflow-x:scroll;border:1px solid black;padding:10px;background-color:#D3D3D3;">';
    echo $this->DisplayXML(false);
    echo '</pre>';
    echo '</body></html>';
}
exit;

/* * ***************************************************************************
 * Create WSDL XML 
 * @PARAM xmlformat=true - Display output in HTML friendly format if set false
 * *************************************************************************** */

function DisplayXML($xmlformat = true) {
    global $functions;         // Functions that this web service supports
    global $url;
    global $types;
    $i = 0;                    // For traversing functions array
    $j = 0;                    // For traversing parameters arrays
    $str = '';                 // XML String to output
    // Tab spacings
    $t1 = '    ';
    if (!$xmlformat)
        $t1 = '&nbsp;&nbsp;&nbsp;&nbsp;';
    $t2 = $t1 . $t1;
    $t3 = $t2 . $t1;
    $t4 = $t3 . $t1;
    $t5 = $t4 . $t1;

    // Declare XML format
    $str .= "<?xml version ='1.0' encoding ='ISO-8859-1' ?>" . "\n\n";

    // Declare definitions / namespaces
    $str .= "<definitions targetNamespace='http://localhost/soap" . $url . "'" . "\n";
    $str .= $t1 . "xmlns:tns='http://localhost/soap" . $url . "'" . "\n";
    $str .= $t1 . "xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'" . "\n";
    $str .= $t1 . "xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'" . "\n";
    $str .= $t1 . "xmlns:xsd='http://www.w3.org/2001/XMLSchema'" . "\n";
    $str .= $t1 . "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" . "\n";
    $str .= $t1 . "xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/'" . "\n";
    $str .= $t1 . "xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'" . "\n";
    $str .= $t1 . "xmlns='http://schemas.xmlsoap.org/wsdl/'" . "\n";
    $str .= '>' . "\n\n";

    // Declare Types / Schema
    $str .= "<types>" . "\n";
    $str .= $t1 . '<xsd:schema targetNamespace="http://localhost/soap' . $url . '">' . "\n";
    $str .= $t2 . '<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />' . "\n";
    $str .= $t2 . '<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />' . "\n";
    for ($i = 0; $i < count($types); $i++) {
        // Define Types
        $str .= $t2 . '<xsd:complexType name="' . $types[$i]['name'] . '">' . "\n";
        $str .= $t3 . '<xsd:' . $types[$i]['name'] . '>' . "\n";
        foreach ($types[$i]['data'] as $key => $value) {
            $str .= $t4 . '<xsd:element ';
            $str .= 'name="' . $types[$i]['data'][$key]['name'] . '" ';
            $str .= 'type="' . $types[$i]['data'][$key]['type'] . '" />' . "\n";
        }
        $str .= $t3 . '</xsd:' . $types[$i]['name'] . '>' . "\n";
        $str .= $t2 . '</xsd:complexType>' . "\n";
    }
    $str .= $t1 . '</xsd:schema>' . "\n";
    $str .= '</types>' . "\n\n";

    // Declare Messages
    for ($i = 0; $i < count($functions); $i++) {
        // Define Request Messages
        if (array_key_exists("inputParams", $functions[$i])) {
            if (count($functions[$i]['inputParams']) > 0) {
                for ($j = 0; $j < count($functions[$i]['inputParams']); $j++) {
                    $str .= '<message name="' . $functions[$i]['funcName'] . 'Request">' . "\n";
                    $str .= $t1 . '<part name="' . $functions[$i]['inputParams'][$j]['name'] . '" type="' . $functions[$i]['inputParams'][$j]['type'] . '" />' . "\n";
                    $str .= '</message>' . "\n";
                }
            } else {
                $str .= '<message name="' . $functions[$i]['funcName'] . 'Request" />' . "\n";
            }
        }
        // Define Response Messages
        if (array_key_exists("outputParams", $functions[$i])) {
            for ($j = 0; $j < count($functions[$i]['outputParams']); $j++) {
                $str .= '<message name="' . $functions[$i]['funcName'] . 'Response">' . "\n";
                $str .= $t1 . '<part name="' . $functions[$i]['outputParams'][$j]['name'] . '" type="' . $functions[$i]['outputParams'][$j]['type'] . '" />' . "\n";
                $str .= '</message>' . "\n\n";
            }
        }
    }

    // Declare Port Types
    for ($i = 0; $i < count($functions); $i++) {
        $str .= '<portType name="' . $url . 'PortType">' . "\n";
        $str .= $t1 . '<operation name="' . $functions[$i]['funcName'] . '">' . "\n";
        if (array_key_exists("inputParams", $functions[$i]))
            $str .= $t2 . '<input message="tns:' . $functions[$i]['funcName'] . 'Request" />' . "\n";
        if (array_key_exists("outputParams", $functions[$i]))
            $str .= $t2 . '<output message="tns:' . $functions[$i]['funcName'] . 'Response" />' . "\n";
        $str .= $t1 . '</operation>' . "\n";
        $str .= '</portType>' . "\n\n";
    }

    // Declare Bindings
    $str .= '<binding name="' . $url . 'Binding" type="tns:' . $url . 'PortType">' . "\n";
    $str .= $t1 . '<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />' . "\n";
    for ($i = 0; $i < count($functions); $i++) {
        $str .= $t1 . '<operation name="' . $functions[$i]['funcName'] . '">' . "\n";
        $str .= $t2 . '<soap:operation soapAction="' . $functions[$i]['soapAddress'] . '/' . $functions[$i]['funcName'] . '" style="rpc" />' . "\n";
        if (array_key_exists("inputParams", $functions[$i]))
            $str .= $t2 . '<input><soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/></input>' . "\n";
        if (array_key_exists("outputParams", $functions[$i]))
            $str .= $t2 . '<output><soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>' . "\n";
        $str .= $t1 . '</operation>' . "\n";
    }
    $str .= '</binding>' . "\n\n";

    // Declare Service
    $str .= '<service name="' . $url . '">' . "\n";
    $str .= $t1 . '<port name="' . $url . 'Port" binding="tns:' . $url . 'Binding">' . "\n";
    $str .= $t2 . '<soap:address location="' . $functions[0]['soapAddress'] . '" />' . "\n";
    $str .= $t1 . '</port>' . "\n";
    $str .= '</service>' . "\n\n";

    // End Document
    $str .= '</definitions>' . "\n";

    if (!$xmlformat)
        $str = str_replace("<", "&lt;", $str);
    if (!$xmlformat)
        $str = str_replace(">", "&gt;", $str);
    if (!$xmlformat)
        $str = str_replace("\n", "<br />", $str);
    return $str;
}
?>